package com.atguigu.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.eduservice.entity.SystemOrganizationBase1;
import com.atguigu.eduservice.entity.excel.OrgExcel;
import com.atguigu.eduservice.service.OrgExcelService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

public class ExcelListener extends AnalysisEventListener<OrgExcel> {
    //因为ExcelListener不交给spring进行管理，需要自己new，不能注入其他对象
    //不能实现数据库操作

    @Resource
    public OrgExcelService orgExcelService;

    public ExcelListener() {

    }

    public ExcelListener(OrgExcelService orgExcelService) {
        this.orgExcelService = orgExcelService;
    }


    //一行一行的读取excel内容
    @Override
    public void invoke(OrgExcel orgExcel, AnalysisContext analysisContext) {
        if (orgExcel ==null) {
            throw new GuliException(20001,"文件数据为空");
        }

        //一行一行读取，每次读取有三个值，第一个值省，第二个值市，第三个值是区
        //判断一级分类是否重复
        SystemOrganizationBase1 yiji = this.existOneProvince(orgExcelService, orgExcel.getProvinceUnit());
        if(yiji == null) { //没有相同省，进行添加
            yiji = new SystemOrganizationBase1();
            yiji.setPid("0");
            yiji.setOrganizationName(orgExcel.getProvinceUnit());//省名称
            orgExcelService.save(yiji);
        }

        //获取一级分类id值
        String organizationBaseId = yiji.getOrganizationBaseId();
        //添加二级分类
        //判断二级分类是否重复
        SystemOrganizationBase1 erji = this.existTwoCity(orgExcelService, orgExcel.getCityUnit(), organizationBaseId);
        if(erji == null) {
            erji = new SystemOrganizationBase1();
            erji.setPid(organizationBaseId);
            erji.setOrganizationName(orgExcel.getCityUnit());//市名称
            orgExcelService.save(erji);
        }

        //获取二级级分类id值
        String organizationBaseId1 = erji.getOrganizationBaseId();
        //添加二级分类
        //判断二级分类是否重复
        SystemOrganizationBase1 sanji = this.existSanCounty(orgExcelService, orgExcel.getCityUnit(), organizationBaseId1);
           if(StringUtils.isNotEmpty(orgExcel.getCityUnit())){
               if(sanji == null) {
                   sanji = new SystemOrganizationBase1();
                   sanji.setPid(organizationBaseId1);
                   sanji.setOrganizationName(orgExcel.getCountyUnit());//市名称
                   orgExcelService.save(sanji);
               }
           }
    }


    private String getCode (String code) {
        return StringUtils.leftPad(code,3, "0");
    }


    //判断省份不能重复添加
    private SystemOrganizationBase1 existOneProvince(OrgExcelService orgExcelService,String name) {
        QueryWrapper<SystemOrganizationBase1> wrapper = new QueryWrapper<>();
        wrapper.eq("organization_name",name);
        wrapper.eq("pid","0");
        SystemOrganizationBase1 oneProvince = orgExcelService.getOne(wrapper);
        return oneProvince;
    }

    //判断市不能重复添加
    private SystemOrganizationBase1 existTwoCity(OrgExcelService orgExcelService,String name,String pid) {
        QueryWrapper<SystemOrganizationBase1> wrapper = new QueryWrapper<>();
        wrapper.eq("organization_name",name);
        wrapper.eq("pid",pid);
        SystemOrganizationBase1 twoCity = orgExcelService.getOne(wrapper);
        return twoCity;
    }


    //判断区不能重复添加
    private SystemOrganizationBase1 existSanCounty(OrgExcelService orgExcelService,String name,String pid) {
        QueryWrapper<SystemOrganizationBase1> wrapper = new QueryWrapper<>();
        wrapper.eq("organization_name",name);
        wrapper.eq("pid",pid);
            SystemOrganizationBase1 sanCounty = orgExcelService.getOne(wrapper);
        return sanCounty;
    }
    //找到省级最后一个添加的
    private String OneLastProvince(OrgExcelService orgExcelService) {
        QueryWrapper<SystemOrganizationBase1> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_code","0");
        wrapper.select().orderByDesc("organization_base_id");
        wrapper.last("limit 1");
        List<SystemOrganizationBase1> list = orgExcelService.list(wrapper);

        String orgCode = null;
        if(list.size()> 0){
            for (int i = 0; i <list.size() ; i++) {
                SystemOrganizationBase1 systemOrganizationBase1 = list.get(i);
                orgCode = systemOrganizationBase1.getOrgCode();
            }

        }
        return orgCode;
    }
    //找到市级最后一个添加的
    private String twoLastCity(OrgExcelService orgExcelService,String orgCode) {
        QueryWrapper<SystemOrganizationBase1> wrapper = new QueryWrapper<>();
        wrapper.eq("org_code",orgCode);
        wrapper.select().orderByDesc("organization_base_id");
        wrapper.last("limit 1");
        List<SystemOrganizationBase1> list = orgExcelService.list(wrapper);
        String orgCode1 = null;
        if(list.size()> 0){
            for (int i = 0; i <list.size() ; i++) {
                SystemOrganizationBase1 systemOrganizationBase1 = list.get(i);
                orgCode1 = systemOrganizationBase1.getOrgCode();
            }

        }
        return orgCode1;
    }

    //找到区最后一个添加的
    private String sanLastCounty(OrgExcelService orgExcelService,String orgCode) {
        QueryWrapper<SystemOrganizationBase1> wrapper = new QueryWrapper<>();
        wrapper.eq("org_code",orgCode);
        wrapper.select().orderByDesc("organization_base_id");
        wrapper.last("limit 1");
        List<SystemOrganizationBase1> list = orgExcelService.list(wrapper);
        String orgCode2 = null;
        if(list.size()> 0){
            for (int i = 0; i <list.size() ; i++) {
                SystemOrganizationBase1 systemOrganizationBase1 = list.get(i);
                orgCode2 = systemOrganizationBase1.getOrgCode();
            }

        }
        return orgCode2;
    }





     //读取表头信息
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        System.out.println("表头"+headMap);
    }


    //读取完成之后做的一些事情
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
